#ifndef __UI_CANNON_LAYOUT_H__
#define __UI_CANNON_LAYOUT_H__
#include "cocos2d.h"
#include "../include/SignalSlot.h"
USING_NS_CC;
namespace fishingjoy
{
	namespace ui
	{
		class UI_CannonLayout:public CCLayer
		{
		public:
			base::Signal<void(void)> Event_CannonAdd;
			base::Signal<void(void)> Event_CannonReduce;
			base::Signal<void(const CCPoint&,float)> Event_TapScreen;
			CCSprite* m_pCannon;
		public:
			UI_CannonLayout()
			{
				m_pCannon = CCSprite::create("ui/pao5.png",CCRectMake(0,0,64,88));
				//bg->setAnchorPoint(CCPointZero);
				addChild(m_pCannon);
				//add button.
				CCMenuItemSprite* pAdd = CCMenuItemSprite::create(CCSprite::create("ui/button_add.png",CCRectMake(0,0,34.5,42))
					,CCSprite::create("ui/button_add.png",CCRectMake(34.5,0,34.5,42)),this,menu_selector(UI_CannonLayout::onMenu_Add));
				pAdd->setPositionX(50);
				//reduce button.
				CCMenuItemSprite* pReduce = CCMenuItemSprite::create(CCSprite::create("ui/button_reduce.png",CCRectMake(0,0,34.5,42))
					,CCSprite::create("ui/button_reduce.png",CCRectMake(34.5,0,34.5,42)),this,menu_selector(UI_CannonLayout::onMenu_Reduce));
				pReduce->setPositionX(-50);
				//add all of buttons into menu.
				CCMenu* pMenu = CCMenu::create(pAdd,pReduce,NULL);
				pMenu->setPositionX(0);
				pMenu->setPositionY(0);
				//attach to ui node.
				addChild(pMenu);

				cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
			}

			void setCannonLevel(int lv)
			{
				m_pCannon->setTextureRect(CCRectMake((lv-1)*64,0,64,88));
			}

		private:
			void onMenu_Add(CCObject* pSender)
			{
				Event_CannonAdd();
			}

			void onMenu_Reduce(CCObject* pSender)
			{
				Event_CannonReduce();
			}

			virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
			{
				//CC_UNUSED_PARAM(pTouch); 
				//CC_UNUSED_PARAM(pEvent);
				cocos2d::CCPoint pt = pTouch->getLocationInView();
				pt = cocos2d::CCDirector::sharedDirector()->convertToGL(pt);

				cocos2d::CCPoint cannonPt = getPosition();

				float dx = pt.x - cannonPt.x;
				float dy = pt.y - cannonPt.y;
				float t = dx/dy;

				float at = atan(t);
				float degree = at/3.1415926*180;
				m_pCannon->setRotation(degree);

				Event_TapScreen(cannonPt,degree);

				//printf("touch %f,%f\n",pTouch->locationInView().x,pTouch->locationInView().y);
				return false;
			}
		};
	}
}
#endif